package com.judian.smart.factory;


import com.judian.smart.base.MyApplication;
import com.judian.smart.model.GateWay;
import com.judian.smart.proxy.WidgetDAOProxy;
import com.judian.smart.service.GateWayService;
import com.judian.smart.util.SystemInfo;

import java.util.ArrayList;
import java.util.List;


public class GateWayFactory {
    private volatile static GateWayFactory install;
    public static List<GateWay> list = new ArrayList<>();

    public static GateWayFactory getInstance() {
        if (null == install) {
            install = new GateWayFactory();
        }
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getGateWay();
        }
        return install;
    }

    public List<GateWay> getAll() {
        try {
            Thread.sleep(50); //此处有个玄学，可能调用时机不当 会慢个几毫秒 此处在主线程睡眠50毫秒（不睡眠会导致专案切换的时候网关显示的数据不一致）
            if (list.size() == 0) {
                list = WidgetDAOProxy.getInstance().getGateWay();
            }
            isMainGateWay();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static void isMainGateWay() {
        int result = 0;
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                GateWay gateWay = list.get(i);
                if (gateWay.getIsCurrent() == 1 && gateWay.getTypeId() <= 3) {
                    result += 1;
                    break;
                }
            }
            if (result == 0) {
                for (int i = 0; i < list.size(); i++) {
                    GateWay gateWay = list.get(i);
                    if (gateWay.getTypeId() == 2 || (gateWay.getTypeId() > 3 && gateWay.getTypeId() < 6)) {
                        gateWay.setIsCurrent(1);
                        WidgetDAOProxy.getInstance().updateIsCurrent(gateWay.getId(), 1);
                        break;
                    }
                }
            }
        }
    }

    public static void clearList() {
        list = WidgetDAOProxy.getInstance().getGateWay();
    }

    public GateWay getGateWay(String deviceID) {
        GateWay gateWay = null;
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getGateWay();
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGatewayID().equals(deviceID)) {
                gateWay = list.get(i);
                break;
            }
        }
        return gateWay;
    }

    public GateWay getGateWayByName(String gateWayName) {
        GateWay gateWay = null;
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getGateWay();
        }

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGatewayName().equals(gateWayName)) {
                gateWay = list.get(i);
                break;
            }
        }
        return gateWay;
    }

    public GateWay getGateWayById(String deviceName) {
        GateWay gateWay = null;
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getGateWay();
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGatewayName().equals(deviceName)) {
                gateWay = list.get(i);
                break;
            }
        }
        return gateWay;
    }

    public String getGateWayByID(String deviceID) {
        String result = "主网关";
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getGateWay();
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGatewayID().equals(deviceID)) {
                result = list.get(i).getGatewayName();
                break;
            }
        }
        return result;
    }

    public GateWay getGateWayByDeviceId(String deviceId) {
        GateWay gateWay = null;
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getGateWay();
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGatewayID().equals(deviceId)) {
                gateWay = list.get(i);
                break;
            }
        }
        return gateWay;
    }

    public boolean idOpen() {
        boolean flag = false;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getTypeId() == 4) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    public List<String> getAllName() {
        List<String> result = new ArrayList<>();
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getGateWay();
        }
        for (int i = 0; i < list.size(); i++) {
            GateWay gateWay = list.get(i);
            if (gateWay.getGatewayID().equals(SystemInfo.getMasterID(MyApplication.getInstance()))) {
                continue;
            }
            result.add(gateWay.getGatewayName());
        }
        return result;
    }

    //	public  List<FileType> getAllByName() {
//		List<FileType> result = new ArrayList<FileType>();
//		if (list.size() == 0) {
//			list = MyApplication.getMInstance().getWidgetDataBase().getGateWay();
//		}
//
//		for (int i = 0; i < list.size(); i++) {
//			GateWay gateWay=list.get(i);
//			FileType fileType=new FileType();
//			fileType.setId(gateWay.getId());
//			fileType.setTypeName(gateWay.getGatewayName());
//			fileType.setTypeText(gateWay.getGatewayID());
//			result.add(fileType);
//		}
//		return result;
//	}
    public List<String> getRemoteControl() {
        List<String> result = new ArrayList<String>();
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getGateWay();
        }
        for (int i = 0; i < list.size(); i++) {
            GateWay gateWay = list.get(i);
            if (gateWay.getTypeId() == 3) {
                result.add(gateWay.getGatewayName());
            }

        }
        return result;
    }

    public boolean isAdmin(String gateWayID) {
        boolean isFlag = false;
        if (list.size() == 0) {
            list = WidgetDAOProxy.getInstance().getGateWay();
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGatewayID().equals(gateWayID)) {
                if (list.get(i).getUserName().equals("admin")) {
                    isFlag = true;
                }
                break;
            }
        }
        return isFlag;
    }

    public List<String> getGateWay() {
        List<String> resultList = new ArrayList<>();
        List<GateWay> list = GateWayService.list;
        for (int i = 0; i < list.size(); i++) {
            GateWay gateWay = list.get(i);
            if (gateWay.getTypeId() < 3) {
                resultList.add(gateWay.getGatewayName() + " (设备" + gateWay.getState() + ")");
            }
        }
        return resultList;
    }

    public List<GateWay> getGateWayMode() {
        List<GateWay> resultList = new ArrayList<>();
        List<GateWay> list = GateWayService.list;
        for (int i = 0; i < list.size(); i++) {
            GateWay gateWay = list.get(i);
            if (gateWay.getTypeId() < 3) {
                resultList.add(gateWay);
            }
        }
        return resultList;
    }

    public String getLineGateWay() {
        for (GateWay gateway : GateWayService.list) {
            if (gateway.getTypeId() < 3 && gateway.getState().equals("在线")) {
                return "";
            }
        }
        return "无网关在线";
    }

    public int getMainGateWay() {
        int result = 0;
        List<GateWay> list = GateWayService.list;
        for (int i = 0; i < list.size(); i++) {
            GateWay gateWay = list.get(i);
            if (gateWay.getTypeId() < 3) {
                result++;
            }
        }
        return result;
    }
}
